<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\Series;
use App\Services\SeriesService;
use Illuminate\Http\Request;
use Inertia\Inertia;

/**
 * 前台系列控制器
 * 
 * 职责：
 * - 处理前台系列相关的HTTP请求
 * - 调用 SeriesService 处理业务逻辑
 * - 返回视图
 */
class SeriesController extends Controller
{
    /**
     * 系列服务
     * 
     * @var SeriesService
     */
    protected SeriesService $seriesService;

    /**
     * 构造器
     */
    public function __construct(SeriesService $seriesService)
    {
        $this->seriesService = $seriesService;
    }

    /**
     * 显示系列列表页
     */
    public function index(Request $request)
    {
        $query = Series::with('author')
            ->visible()
            ->orderBy('created_at', 'desc');

        // 搜索过滤
        if ($request->has('search')) {
            $search = $request->input('search');
            $query->where(function($q) use ($search) {
                $q->where('name', 'like', "%{$search}%")
                  ->orWhere('description', 'like', "%{$search}%");
            });
        }

        $series = $query->paginate(12)->withQueryString();


        return Inertia::render('Blog/Series/Index', [
            'series' => $series,
            'filters' => [
                'search' => $request->input('search'),
            ],
        ]);
    }

    /**
     * 显示系列详情页
     */
    public function show(Series $series)
    {
        // 检查系列是否可见
        if (!$series->is_visible) {
            abort(404);
        }
        // 获取系列文章（根据 sort_mode 排序）
        $posts = $this->seriesService->getPosts($series);
        $seriesList = Series::visible()
            ->orderBy('created_at', 'desc')
            ->get(['id', 'name', 'slug']);

        return Inertia::render('Blog/Series/Show', [
            'series' => [
                'id' => $series->id,
                'name' => $series->name,
                'slug' => $series->slug,
                'description' => $series->description,
                'featured_image' => $series->featured_image,
                'post_count' => $series->post_count,
                'created_at' => $series->created_at?->format('Y-m-d H:i:s'),
                'updated_at' => $series->updated_at?->format('Y-m-d H:i:s'),
                'author' => [
                    'name' => $series->author->name,
                    'avatar' => $series->author->profile_photo_url,
                ],
            ],
            'seriesList' => $seriesList->map(function ($item) use ($series) {
                return [
                    'id' => $item->id,
                    'name' => $item->name,
                    'slug' => $item->slug,
                    'is_current' => $item->id === $series->id,
                ];
            }),
            'posts' => $posts->map(function($post) {
                return [
                    'id' => $post->id,
                    'title' => $post->title,
                    'slug' => $post->slug,
                    'excerpt' => $post->excerpt,
                    'featured_image' => $post->featured_image,
                    'published_at' => $post->published_at?->format('Y-m-d H:i:s'),
                    'views' => $post->views,
                    'series_order' => $post->series_order,
                ];
            }),
        ]);
    }
}
